<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">

      <el-form-item label="关联权益" prop="interestId">
        <el-select v-model="queryParams.interestId" placeholder="请选择权益" clearable>
          <el-option v-for="item in interestList" :key="item.id" :label="`${item.name}|${item.code}`"
            :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="三方订单号" prop="jtcOrderNo">
        <el-input v-model="queryParams.jtcOrderNo" placeholder="请输入关联订单" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="发放状态" prop="saleStatus">
        <el-select v-model="queryParams.saleStatus" placeholder="请选择发放状态" clearable>
          <el-option label="待售" value="0"></el-option>
          <el-option label="已售" value="1"></el-option>
          <el-option label="已领取" value="2"></el-option>
          <el-option label="已过期" value="3"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model="queryParams.mobile" placeholder="请输入手机号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="领取时间 " prop="receiveTime">
        <el-date-picker clearable v-model="queryParams.receiveTime" type="datetimerange" value-format="yyyy-MM-dd"
          placeholder="请选择下单时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="codeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="权益编码" align="center" prop="interestCode" />
      <el-table-column label="权益名称" align="center" prop="interestName" />
      <el-table-column label="兑换码" align="center" prop="code" />
      <el-table-column label="发放状态" align="center" prop="saleStatus">
        <template slot-scope="scope">
          <span v-if="scope.row.saleStatus == '0'">待售</span>
          <span v-if="scope.row.saleStatus == '1'">已售</span>
          <span v-if="scope.row.saleStatus == '2'">已领取</span>
          <span v-if="scope.row.saleStatus == '3'">已过期</span>
        </template>
      </el-table-column>
      <el-table-column label="手机号" align="center" prop="mobile" />
      <el-table-column label="有效期" align="center">
        <template slot-scope="scope">
          {{ scope.row.startTime }}至{{ scope.row.endTime }}
        </template>
      </el-table-column>
      <el-table-column label="领取时间" align="center" prop="receiveTime" />
      <el-table-column label="订单号" align="center" prop="orderNo" />
      <el-table-column label="三方订单号" align="center" prop="jtcOrderNo" />
      <el-table-column label="商品编码" align="center" prop="goodsNo" />
      <el-table-column label="分配状态" align="center" prop="status">
        <template slot-scope="scope">
          <span v-if="scope.row.status == '0'">未分配</span>
          <span v-if="scope.row.status == '1'">已分配</span>
        </template>
      </el-table-column>
      <el-table-column label="分配次数" align="center" prop="payCount" />
      <el-table-column label="分配记录" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleView(scope.row)"
            v-hasPermi="['business:interest:query']">查看</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <el-dialog :title="title" :visible.sync="open" width="100%" append-to-body>
      <PayRecord :code="code"></PayRecord>
    </el-dialog>
  </div>
</template>

<script>
import { listCode, getCode } from "@/api/business/code";
import { listAllInterest } from '@/api/business/interest';
import PayRecord from '../payrecord/index'

export default {
  name: "Code",
  components: { PayRecord },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 权益兑换码表格数据
      codeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        interestId: null,
        saleStatus: null,
        orderNo: null,
        jtcOrderNo: null,
        mobile: null,
        receiveTime: [],
        startTime: '',
        endTime: ''
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        code: [
          { required: true, message: "兑换码不能为空", trigger: "blur" }
        ],
        interestId: [
          { required: true, message: "关联权益不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "分配状态", trigger: "change" }
        ],
      },
      interestList: [],

      code: '',
    };
  },
  created() {
    this.listAllInterest();
    this.getList();
  },
  methods: {
    handleView(row) {
      this.open = true;
      console.log("row:", row);
      this.code = row.code;
    },
    listAllInterest() {
      listAllInterest({}).then(res => {
        this.interestList = res.data
      })
    },
    /** 查询权益兑换码列表 */
    getList() {
      this.loading = true;
      if (this.queryParams.receiveTime.length > 0) {
        this.queryParams.startTime = this.queryParams.receiveTime[0];
        this.queryParams.endTime = this.queryParams.receiveTime[1];
      }
      this.$set(this.queryParams, 'receiveTime', '');
      listCode(this.queryParams).then(response => {
        this.codeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        code: null,
        interestId: null,
        status: null,
        saleStatus: null,
        orderNo: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.receiveTime = [];
      this.queryParams.startTime = '';
      this.queryParams.endTime = '';
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加权益兑换码";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCode(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改权益兑换码";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCode(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCode(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除权益兑换码编号为"' + ids + '"的数据项？').then(function () {
        return delCode(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/code/export', {
        ...this.queryParams
      }, `code_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
